package com.hoshiicloud.payment.feign;

import com.hoshiicloud.payment.feign.fallback.DownloadServiceFeignFallbackFactory;
import feign.Logger;
import feign.Response;
import feign.codec.Decoder;
import feign.form.spring.converter.SpringManyMultipartFilesReader;
import java.util.ArrayList;
import lombok.val;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @program: hoshiicloud
 * @description: 测试
 * @author: Mr.wu
 * @create: 2019-06-04 16:28
 **/
@FeignClient(name = "hoshiicloud-fileserver", fallbackFactory = DownloadServiceFeignFallbackFactory.class)
public interface DownloadService {

    /**
     * @param bucketName
     * @param objectKey
     * @return com.hoshiicloud.common.rpc.Response
     * @author: Mr.wu
     * @descripton: 上传文件
     * @date: 17:00 2019/6/5
     */
    @RequestMapping(value = "/file/downloadFile", method = RequestMethod.GET)
    Response fileDownload(@RequestParam("bucketName") String bucketName,
            @RequestParam("objectKey") String objectKey);

    class ClientConfiguration {

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Decoder feignDecoder() {
            val springConverters = messageConverters.getObject().getConverters();
            val decoderConverters = new ArrayList<HttpMessageConverter<?>>(
                    springConverters.size() + 1);

            decoderConverters.addAll(springConverters);
            decoderConverters.add(new SpringManyMultipartFilesReader(4096));

            val httpMessageConverters = new HttpMessageConverters(decoderConverters);

            return new SpringDecoder(() -> httpMessageConverters);
        }

        @Bean
        public Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
}
